package cn.iluwen.airline.Services;

import cn.iluwen.airline.Entities.UserRegistration;
import cn.iluwen.airline.Mapper.UserRegistrationMapper;
import cn.iluwen.airline.utils.BCrypt;
import cn.iluwen.airline.utils.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 用户注册服务类
 * 处理用户注册相关的业务逻辑
 */
@Service
public class UserRegistrationService {
    
    @Autowired
    private UserRegistrationMapper registrationMapper;
    
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    /**
     * 执行用户注册，创建用户注册记录
     * @param username 用户名
     * @param email 邮箱
     * @param password 密码
     * @param phone 手机号码
     * @param realName 真实姓名
     * @param idCard 身份证号
     * @param registrationIp 注册IP
     * @param registrationSource 注册来源
     * @return 注册信息对象
     */
    @Transactional
    public UserRegistration register(String username, String email, String password, String phone, 
                                    String realName, String idCard, 
                                    String registrationIp, String registrationSource) {
        // 检查邮箱是否已存在
        if (registrationMapper.findByEmail(email) != null) {
            throw new RuntimeException("邮箱已被注册");
        }
        
        // 检查用户名是否已存在
        if (registrationMapper.findByUsername(username) != null) {
            throw new RuntimeException("用户名已被注册");
        }
        
        // 创建注册记录
        UserRegistration registration = new UserRegistration();
        registration.setRegistrationId(UUID.randomUUID().toString());
        registration.setRegistrationDate(LocalDateTime.now());
        
        // 加密密码
        registration.setPassword(BCrypt.encrypt(password));
        
        // 设置用户信息
        registration.setUsername(username);
        registration.setEmail(email);
        registration.setPhone(phone);
        registration.setRealName(realName);
        registration.setIdCard(idCard);
        registration.setRegistrationIp(registrationIp);
        registration.setRegistrationSource(registrationSource);
        
        // 保存注册记录
        registrationMapper.createRegistration(registration);
        
        return registration;
    }
    
    /**
     * 通过邮箱查找注册记录
     * @param email 邮箱
     * @return 注册记录
     */
    public UserRegistration findByEmail(String email) {
        return registrationMapper.findByEmail(email);
    }
    
    /**
     * 通过用户名查找注册记录
     * @param username 用户名
     * @return 注册记录
     */
    public UserRegistration findByUsername(String username) {
        return registrationMapper.findByUsername(username);
    }
    
    /**
     * 根据ID查询注册记录
     * @param registrationId 注册ID
     * @return 注册记录
     */
    public UserRegistration findById(String registrationId) {
        return registrationMapper.findById(registrationId);
    }
    
    /**
     * 获取所有注册信息
     * @return 注册列表
     */
    public List<UserRegistration> getAllRegistrations() {
        return registrationMapper.findAllRegistrations();
    }
    
    /**
     * 分页获取所有注册信息
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页后的注册列表
     */
    public PageInfo<UserRegistration> getRegistrationsWithPaging(int pageNum, int pageSize) {
        PageUtils.startPage(pageNum, pageSize);
        List<UserRegistration> registrations = registrationMapper.findRegistrationsWithPaging();
        return PageUtils.getPageInfo(registrations);
    }
    
    /**
     * 根据日期范围分页查询注册信息
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页后的注册列表
     */
    public PageInfo<UserRegistration> getRegistrationsByDateRange(LocalDateTime startDate, LocalDateTime endDate, int pageNum, int pageSize) {
        PageUtils.startPage(pageNum, pageSize);
        String startDateStr = startDate.format(DATE_FORMATTER);
        String endDateStr = endDate.format(DATE_FORMATTER);
        List<UserRegistration> registrations = registrationMapper.findRegistrationsByDateRange(startDateStr, endDateStr);
        return PageUtils.getPageInfo(registrations);
    }
    
    /**
     * 根据注册来源分页查询注册信息
     * @param source 注册来源
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页后的注册列表
     */
    public PageInfo<UserRegistration> getRegistrationsBySource(String source, int pageNum, int pageSize) {
        PageUtils.startPage(pageNum, pageSize);
        List<UserRegistration> registrations = registrationMapper.findRegistrationsBySource(source);
        return PageUtils.getPageInfo(registrations);
    }
    
    /**
     * 删除注册记录
     * @param registrationId 注册ID
     * @return 是否删除成功
     */
    @Transactional
    public boolean deleteRegistration(String registrationId) {
        return registrationMapper.deleteRegistration(registrationId) > 0;
    }
    
    /**
     * 验证密码
     * @param registration 注册记录
     * @param plainPassword 明文密码
     * @return 验证结果
     */
    public boolean verifyPassword(UserRegistration registration, String plainPassword) {
        return BCrypt.matches(plainPassword, registration.getPassword());
    }
} 